package com.maoye.ossp.sys.quartz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.quartz.entity.SysQuartzJob;
import com.maoye.ossp.sys.quartz.service.JobService;

@Controller()
@RequestMapping(value = "sys/quartz/job")
public class JobController extends BaseController {
	
	@Resource
	private JobService service;
	
	@RequestMapping(value = "getJobList")
	public String getJobList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<?> list = service.getJob(map);
		modelMap.put("list", list);
		return "/sys/quartz/jobList";
	}
	
	@RequestMapping(value = "getJob")
	public String getJob(HttpServletRequest request, HttpServletResponse response, Long id, ModelMap modelMap) {
		SysQuartzJob job = service.getJobByID(id);
		modelMap.put("job", job);
		return "/sys/quartz/jobView";
	}
	
	@RequestMapping(value="addJob")
	public String addJob(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes, SysQuartzJob job, ModelMap modelMap) {
		job.setId(idGenerator.getUId());
		service.addJob(job);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:getJobList";
	}
	
	@RequestMapping(value="upJobStatus")
	public String upJobStatus(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes, SysQuartzJob job, ModelMap modelMap) {
		service.upJobStatus(job);
		return "redirect:getJobList";
	}
	
	@RequestMapping(value = "showAddPage")
	public String showAddPage(SysQuartzJob job, ModelMap map) {
		map.put("job", job);
		return "/sys/quartz/addJob";
	}
	
	@RequestMapping(value = "upJob")
	public String upJob(SysQuartzJob job, ModelMap map, RedirectAttributes redirectAttributes) {
		service.upJob(job);
		addMessage(redirectAttributes, "修改成功");
		return "redirect:getJobList";
	}
	
	@RequestMapping(value = "forthwith")
	public String forthwith(String id, String clazz, ModelMap map, RedirectAttributes redirectAttributes) {
		service.forthwith(id,clazz);
		addMessage(redirectAttributes, "执行成功");
		return "redirect:getJobList";
	}
}
